#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : Tue Jul 23 2019
# @Author  : xujunfu (xjf891121@gmail.com})
# @Link    : http://junfuxu.github.io/
import uuid
from datetime import datetime

from . import config


class ZSHApi:
    VENDER_CODE = 'YBAD'
    FORMAT_DT = '%Y-%m-%d %H:%M:%S'
    URL_BASE = 'https://219.143.119.224/RD/'
    FILE_STATUS = 'start_ids.json'
    COORDINATE = 'GCJ02'
    LIMIT = 5000

    def __init__(self, *args, **kwargs):
        self.headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json, text/plain, */*',
            'Date':  datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'),
            'apiName': 'youbanganda',
            'apiToken': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.jLHatDKHnNd8V1HGxbIEdlBKU9PjN_0ovJfQCvw9Z9A',
        }
        self.logger = config.get_logger('zsh-api')

    def send(self, method_name, item_list):
        if item_list:
            url = f'{self.BASE_URL}{method_name}'
            uid = str(uuid.uuid1())
            data = {
                "msgId": str(uid),
                method_name: item_list
            }
            self.logger.info(f'>>> DO POST {method_name}, data_len={len(item_list)}, date={data}')
            # resp = requests.post(url=url, data={method_name: json.dumps(data, ensure_ascii=False)}, headers=self.headers, verify=False)
            # if resp.json()['responseCode'] != 200:
            #     raise ValueError(f'<<< ERROR RESP:{resp.json()}')
